using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Diagnostics;
using System.Threading;
using System.Collections.Specialized;
using Blazing.BusinessObjects.Cms;

using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;
using ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;


namespace BusinessObjectTests
{
    [TestClass]
    public class SpeedTests
    {
        [TestMethod, Ignore]
        public void NamedMutexTest()
        {
            Mutex m1 = null;
            Mutex m2 = null;
            try{
                bool isFirst;
                m1 = new Mutex(true, "test1", out isFirst);

                bool wasLoaded;
                m2 = new Mutex(true, "test1", out wasLoaded);

                Assert.AreEqual(true, isFirst);
                Assert.AreEqual(false, wasLoaded);

                ThreadStart s = new ThreadStart(delegate(){
                    Trace.WriteLine("Delegate wait...");
                    Thread.Sleep(100);
                    m1.ReleaseMutex();
                });

                Assert.AreEqual(true, m1.WaitOne(-1, false));
                new Thread(s).Start();

                if (!m2.WaitOne())
                {
                    Assert.Fail("Expected wait to return true");
                }
                else
                {
                    Trace.WriteLine("Main wait...");
                    Thread.Sleep(1000);
                    m2.ReleaseMutex();
                }

                Trace.WriteLine("Done...");
                Thread.Sleep(2000);
            }
            finally{
                m1.Close();
                m2.Close();
            }
        }

        [TestMethod]
        [Ignore]
        public void DictionaryIsFaster()
        {
            DateTime start = DateTime.Now;
            Hashtable hash = new Hashtable();
            for (int i = 0; i < 200; i++)
            {
                hash[i + "item"] = i;
            }
            TimeSpan stop = DateTime.Now - start;

            start = DateTime.Now;
            Dictionary<string, object> dict = new Dictionary<string, object>();
            for (int i = 0; i < 200; i++)
            {
                dict[i + "item"] = i;
            }
            TimeSpan stop2 = DateTime.Now - start;

            start = DateTime.Now;
            ListDictionary list = new ListDictionary();
            for (int i = 0; i < 200; i++)
            {
                list[i + "item"] = i;
            }
            TimeSpan stop3 = DateTime.Now - start;

            Trace.WriteLine(string.Format("Hash: {0}, Dict: {1}, ListDict: {2}", stop.Ticks, stop2.Ticks, stop3.Ticks));
        }

        [TestMethod]
        [Ignore]
        public void DictionaryIsFasterCollective()
        {
            //Test populating 500 objects with 100 properties

            TimeSpan c1 = new TimeSpan();
            TimeSpan c2 = new TimeSpan();
            TimeSpan c3 = new TimeSpan();

            for (int j = 0; j < 20000; j++)
            {
                DateTime start = DateTime.Now;
                Hashtable hash = new Hashtable();
                for (int i = 0; i < 100; i++)
                {
                    hash[i + "item"] = i;
                }
                TimeSpan stop = DateTime.Now - start;
                c1.Add(stop);

                start = DateTime.Now;
                Dictionary<string, object> dict = new Dictionary<string, object>();
                for (int i = 0; i < 100; i++)
                {
                    dict[i + "item"] = i;
                }
                TimeSpan stop2 = DateTime.Now - start;
                c2.Add(stop2);

                start = DateTime.Now;
                ListDictionary list = new ListDictionary();
                for (int i = 0; i < 100; i++)
                {
                    list[i + "item"] = i;
                }
                TimeSpan stop3 = DateTime.Now - start;
                c3.Add(stop3);
            }

            Trace.WriteLine(string.Format("Hash: {0}, Dict: {1}, ListDict: {2}", c1.Ticks, c2.Ticks, c3.Ticks));
        }

    }
}
